package com.cyw.common.domain.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * 服务运行模式
 */
@Getter
@AllArgsConstructor
public enum RuntimeMode {

    LOCAL(-1, "本地模式", "local"),
    DEV(0, "开发模式", "dev"),
    TEST(1, "测试模式", "test"),
    PRE(3, "预发模式", "pre"),
    PRODUCTION(2, "生产模式", "prod");

    private final Integer value;
    private final String desc;
    private final String name;

    private static final Map<Integer, RuntimeMode> MAP = new HashMap<>(values().length);
    private static final Map<String, RuntimeMode> NAME_MAP = new HashMap<>(values().length);

    static {
        for (RuntimeMode item : values()) {
            MAP.put(item.value, item);
            NAME_MAP.put(item.getName(), item);
        }
    }

    public static RuntimeMode nameOf(String name) {
        RuntimeMode mode = NAME_MAP.get(name);
        return mode == null ? LOCAL : mode;
    }

    public static RuntimeMode valueOf(Integer runtimeMode) {
        RuntimeMode mode = MAP.get(runtimeMode);
        return mode == null ? LOCAL : mode;
    }

    /**
     * 严格模式
     */
    public static boolean isStrict(Integer value) {
        return value == null || value == PRODUCTION.value || value == PRE.value;
    }

    public static boolean isProduction(Integer value) {
        return value != null && value == PRODUCTION.value;
    }

    public static boolean isPre(Integer value) {
        return value != null && value == PRE.value;
    }

    public static boolean isTest(Integer value) {
        return value != null && value == TEST.value;
    }

    public static boolean isDev(Integer value) {
        return value != null && value == DEV.value;
    }

    public static boolean isLocal(Integer value) {
        return value != null && value == LOCAL.value;
    }
}
